package com.bfxy.rabbit.consumer.component;

import com.rabbitmq.client.Channel;
import org.springframework.amqp.rabbit.annotation.*;
import org.springframework.amqp.support.AmqpHeaders;
import org.springframework.messaging.Message;
import org.springframework.stereotype.Component;

import java.io.IOException;

/**
 * @author caryguan
 * @version V1.0
 * @Package com.bfxy.rabbit.consumer.component
 * @date 2020/9/30 10:16
 */
@Component
public class RabbitReceive {

    /**
     * @param message
     * @param channel
     */
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(value = "queue-1", durable = "true"),
            exchange = @Exchange(name = "exchange-1",
                    durable = "true",
                    type = "topic",
                    ignoreDeclarationExceptions = "true"),
            key = "springboot.*"
    ))
    @RabbitHandler
    public void onMessage(Message message, Channel channel) throws IOException {
        //1.收到消息后进行业务端消费处理
        System.out.println("===============================");
        System.out.println("消费消息:" + message.getPayload());
        //2.处理成功之后,获取deliveryTag,进行手动的ack操作,因为我们配置是手动确认模式
        Long deliveryTag = (Long) message.getHeaders().get(AmqpHeaders.DELIVERY_TAG);
        //手动模式手工返回ack
        channel.basicAck(deliveryTag,false);
    }
}